package com.yanyeori.framework.web.http.request.okhttp;

import com.yanyeori.framework.web.http.request.GeneralRequestHeader;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.util.Map;

/**
 * OkHttp拦截器，请求headers处理
 *
 * @author chenkuan
 * @since 2022/11/28
 */
public class OkHttpRequestInterceptor extends GeneralRequestHeader implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();

        Map<String, String> headersMap = contextsToHeadersMap(getContext());
        if (!headersMap.isEmpty()) {
            Request.Builder builder = request.newBuilder();
            headersMap.forEach(builder::addHeader);
            request = builder.build();
        }

        return chain.proceed(request);
    }
}
